import pandas as pd

wD = pd.read_excel('WindData_whole.xls')


#风向频率计算
'''
N:348.25-360,0-11.25
NNE:11.25-33.75
NE:33.75-56.25
ENE:56.25-78.75

E:78.75-101.25
ESE:101.25-123.75
SE:123.75-146.25
SSE:146.25-168.75

S:168.75-191.25
SSW:191.25-213.75
SW:213.75-236.25
WSW:236.25-258.75

W:258.75-281.25
WNW:281.25-303.75
NW:303.75-326.25
NNW:326.25-348.75
'''

freqOfPowerdDir = pd.DataFrame()
dirFreq={'N':0,'NNE':0,'NE':0,'ENE':0,'E':0,'ESE':0,'SE':0,'SSE':0,'S':0,'SSW':0,'SW':0,'WSW':0,'W':0,'WNW':0,'NW':0,'NNW':0}
powerFreq={'N':0,'NNE':0,'NE':0,'ENE':0,'E':0,'ESE':0,'SE':0,'SSE':0,'S':0,'SSW':0,'SW':0,'WSW':0,'W':0,'WNW':0,'NW':0,'NNW':0}
n=0
for deg in wD['CH6Avg']:
    #print(deg)

    if deg>=11.25 and deg<33.75:
        dirFreq['NNE']+=1
        powerFreq['NNE'] += float(wD['CH1Avg'][n:n+1].values**3)
        #print(wfNNE)
    elif deg>=33.75 and deg<56.25:
        dirFreq['NE']+=1
        powerFreq['NE'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=56.25 and deg<78.75:
        dirFreq['ENE']+=1
        powerFreq['ENE'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=78.75 and deg<101.25:
        dirFreq['E']+=1
        powerFreq['E'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=101.25 and deg<123.75:
        dirFreq['ESE']+=1
        powerFreq['ESE'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=123.75 and deg<146.25:
        dirFreq['SE']+=1
        powerFreq['SE'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=146.25 and deg<168.75:
        dirFreq['SSE']+=1
        powerFreq['SSE'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=168.75 and deg<191.25:
        dirFreq['S']+=1
        powerFreq['S'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=191.25 and deg<213.75:
        dirFreq['SSW']+=1
        powerFreq['SSW'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=213.75 and deg<236.25:
        dirFreq['SW']+=1
        powerFreq['SW'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=236.25 and deg<258.75:
        dirFreq['WSW']+=1
        powerFreq['WSW'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=258.75 and deg<281.25:
        dirFreq['W']+=1
        powerFreq['W'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=281.25 and deg<303.75:
        dirFreq['WNW']+=1
        powerFreq['WNW'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=303.75 and deg<326.25:
        dirFreq['NW']+=1
        powerFreq['NW'] += float(wD['CH1Avg'][n:n+1].values**3)
    elif deg>=326.25 and deg<348.75:
        dirFreq['NNW']+=1
        powerFreq['NNW'] += float(wD['CH1Avg'][n:n+1].values**3)
    else:
        dirFreq['N']+=1
        #print(wD.loc[wD['CH6Avg']==deg].index)
        powerFreq['N'] += float(wD['CH1Avg'][n:n+1].values**3)
    
    n+=1

#风向频率
print(dirFreq)
print('n:',n)
print('各方向风向频率:')
print(type(dirFreq['N']))
for deg in dirFreq:
    #print(deg,':',dirFreq[deg]/n)
    dirFreq[deg]=format(dirFreq[deg]/n,'.3f')
    print(deg,':',dirFreq[deg])
print(type(dirFreq['N']))
windDirFreq=pd.DataFrame(dirFreq,index=[0])
windDirFreq.to_excel('风向频率.xlsx')



sumP=0
for dir in powerFreq:
    sumP += powerFreq[dir]

print(sumP)
print('各方向风能频率:')

for dir in powerFreq:
    powerFreq[dir]=format(powerFreq[dir]/sumP,'.3f')
    print(dir,':',powerFreq[dir])
print(type(powerFreq[dir]))
windPowerFreq=pd.DataFrame(powerFreq,index=[0])
windPowerFreq.to_excel('风能频率.xlsx')







